<template>
  <div class="box">
    <div class="title">巨人检测数据单在线验证</div>
    <div class="sbox" v-if="pdfData">
      <p>数据单编号：{{ pdfData.code }}</p>
      <p>受检方：{{ pdfData.unit_name }}</p>
      <p>检测单位：天津巨人检测有限责任公司</p>
      <p>原始单据照片：<div class="imgBox">
            <div class="img" v-for="item in pdfData.simages.split(',')" :key="item">
              <img  :src="item" @click="handleView(item)"/>
            </div>
        </div>
      </p>
      <p>样品照片：
        <div class="imgBox">
            <div class="img" v-for="item in pdfData.yimages.split(',')" :key="item">
              <img  :src="item" @click="handleView(item)"/>
            </div>
        </div>
      </p>
      <p>快检员证件照片：
        <div class="imgBox">
            <div class="img" v-for="item in pdfData.image.split(',')" :key="item">
              <img  :src="item" @click="handleView(item)"/>
            </div>
        </div>
      </p>
      <p>检测日期：{{ pdfData.detection_time_text }}</p>
    </div>

    <preview v-model="showPreview" :url="previewUrl" />
    <div class="down">
      <div class="button" @click="handleToPdf">查看报告</div>
    </div>
  </div>
</template>

<script setup>
  import { ref, onMounted } from 'vue';
  import { useRoute, useRouter } from 'vue-router';
  import axios from 'axios';
import Preview from './preview.vue'
  const pdfData = ref(''); // pdf数据
  const pdfurl = ref('');
  const router = useRouter();

  const route = useRoute();
  const showPreview = ref(false)
  const previewUrl = ref('')

  const sid = ref('')

  const handleToPdf = () => {
    router.push({ path: '/html2pdf', query: { id: sid.value,show:1 } });
  };

  const handleView = (url) => {
  previewUrl.value = url
  showPreview.value = true
}

  // 获取数据的函数
  const fetchData = async id => {
    try {
      const response = await axios.get(
        `https://g.gdimall.com/api/index/getInfo?id=${id}`
      );
      pdfData.value = response.data.data.info[0]; // 假设后端返回的是数组
      pdfurl.value = response.data.data.pdfurl; // 假设后端返回的是数组
    } catch (error) {
      console.error('获取数据失败:', error);
    }
  };

  const downReport = async () => {
    const url = pdfurl.value;
    const link = document.createElement('a');
    link.href = url;
    link.setAttribute('download', `巨人检测数据单.pdf`);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(url);
  };

  onMounted(async () => {
    // 检查URL参数
    document.title = '巨人检测数据单在线验证';
    sid.value = route.query.id;
    const id = route.query.id;
    await fetchData(id);
  });
</script>

<style scoped lang="scss">
  .box {
    max-width: 600px;
    margin: 0 auto;
    padding: 30px;
    box-sizing: border-box;
    .title {
      font-size: 18px;
      text-align: center;
      margin-bottom: 20px;
    }
    .sbox {
      background: #f8f8f8;
      border: 1px solid #eee;
      padding: 15px;
      p {
        font-size: 14px;
        color: #545454;
        line-height: 30px;
        margin-bottom: 10px;
        display: flex;
        img {
          width: 50px;
          height: 50px;
          border-radius: 5px;
        }
      }
    }
    .button {
      width: 100%;
      height: 40px;
      background: #4e73df;
      color: #fff;
      border: none;
      text-align: center;
      line-height: 40px;
      cursor: pointer;
      margin-top: 20px;
    }
  }
</style>
